//
//  ZWTabBar.m
//  oc-wei
//
//  Created by xue-iOS-work on 16/8/30.
//  Copyright © 2016年 xue. All rights reserved.
//

#import "ZWTabBar.h"
@interface ZWTabBar ()

@property (nonatomic, strong) UIButton *btn;



@end

@implementation ZWTabBar

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}
// 初始化
- (void)setupUI {
    [self addSubview:self.btn];
 
    self.backgroundImage = [UIImage imageNamed:@"tabbar_background"];
}
// 重新布局
- (void)layoutSubviews {
    [super layoutSubviews];
   self.btn.center = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.5);
    // 计算宽
    CGFloat w = [UIScreen mainScreen].bounds.size.width / 5;
    CGFloat h = self.frame.size.height;
    CGFloat y = 0;

    // 定义下标
    int index = 0;
    for (UIView *subView in self.subviews) {
        NSLog(@"%@", self.subviews);
        if (![subView isKindOfClass:[UIControl class]]||subView==self.btn)continue; {
            //计算x
            CGFloat x = (CGFloat)index * w;
            // 设置他的frame
            subView.frame = CGRectMake(x, y, w, h);
            index++;
            if (index == 2) {
                index++;
            }
        }
    }
}
- (void)clickMidBtn {
    if(self.clickBtn)
    {
        self.clickBtn();
    }
}
//-------懒加载
-(UIButton *)btn
{
    if (_btn==nil) {
        _btn = [[UIButton alloc]init];
        // 设置背景图片
        [_btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
        [_btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        // 设置图片
        [_btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        [_btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        [_btn sizeToFit];
        // 监听事件
        [_btn addTarget:self action:@selector(clickMidBtn) forControlEvents:UIControlEventTouchUpInside];

    }
    return _btn;
}
@end
